<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\System;

/**
* 系统配置类
* @author   sam 
*/
class SystemConf {

    private $model = null;
    private $id = null;

    public function __construct($id)
    {
        $this->model = M("setting");
        $this->id = $id;
    }

    /**
     * 新增配置信息
     * @param $data
     * @return mixed
     */
    public static function create($data)
    {
        $data['createtime'] = date("Y-m-d H:i:s");
        return M("setting")->add($data);
    }

    /**
     * 获取配置详细信息
     * @return mixed
     */
    public function detail()
    {
        return $this->model->where("id=".$this->id)->find();
    }

    /**
     * 更新配置信息
     * @param $data
     * @return bool
     */
    public function update($data)
    {
        $data['updatetime'] = date('Y-m-d H:i:s');
        $ret = $this->model->where(['id'=>$this->id])->save($data);
        return $ret;
    }

    /**
     * 获取系统配置
     * @param $name
     * @return bool|mixed
     */
    public static function getConfByName($name)
    {
        if(empty($name)){
           return false;
        }
        $conf =  M("setting")->where(['name'=>trim($name)])->find();
        return $conf;
    }

    /**
     * @param string $name
     * @param int $isJson
     * @return mixed
     */
    public static function getConfValueByName($name,$isJson = 0)
    {
        if(empty($name)){
            return false;
        }
        $conf =  M("setting")->where(['name'=>trim($name)])->find();
        if($isJson==1){
            return json_decode($conf['value'],true);
        }
        return $conf['value'];
    }

    /**
     * 更新系统配置
     */
    public static function updateConfByValue($name,$value)
    {
        if(empty($name) || empty($value)){
            return false;
        }
        return M('setting')->where(['name'=>trim($name)])->save(['updatetime'=>date('Y-m-d H:i:s',time()),'value'=>$value]);
    }

    public static function getList($page,$limit)
    {
        $page = intval($page) > 0 ? intval($page) : 1;
        $limit = intval($limit) > 0 ? intval($limit) : 10;
        $map = [];

        if(!empty($location_area)){
            $map['c.location_area'] = array('in',$location_area);
        }

        $total = M("setting")->where($map)->count();
        $list = M("setting")->where($map)->page($page)->limit($limit)->order("id desc")->select();
        //echo M()->getLastSql();

        $data = ['total'=>$total,'list'=>$list];
        return $data;
    }

}